package 力扣._41_缺失的第一个正数;

class Solution2 {
    //官方题解——交换
    public int firstMissingPositive(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            while (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
                int t = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = t;
            }
        }
        for (int i = 0; i < n; i++) {
            if (nums[i] != i + 1)
                return i + 1;
        }
        return n + 1;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{4, 5, 6, 7, 1, 2, 3};
        int i = new Solution2().firstMissingPositive(nums);
        System.out.println(i);
    }
}
